package com.zhentao.pojo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.zhentao.loginUser.pojo.TMenu;
import com.zhentao.loginUser.pojo.TRole;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 
 * @TableName t_user
 */
@TableName(value ="t_user")
@Data
public class TUser implements Serializable, UserDetails {
    /**
     * 用户编号
     */
    @TableId(value = "user_id", type = IdType.AUTO)
    private Integer userId;

    /**
     * 账号
     */
    @TableField(value = "username")
    private String username;

    /**
     * 密码
     */
    @TableField(value = "password")
    private String password;

    /**
     * 用户名
     */
    @TableField(value = "nickname")
    private String nickname;

    /**
     * 头像
     */
    @TableField(value = "avatar")
    private String avatar;

    /**
     * 班级外键
     */
    @TableField(value = "class_id")
    private Integer classId;

    /**
     * 创建时间
     */
    @TableField(value = "create_time")
    private Date createTime;

    /**
     * 修改时间
     */
    @TableField(value = "update_time")
    private Date updateTime;

    /**
     * 手机号
     */
    @TableField(value = "phone")
    private String phone;

    /**
     * 市场部外键
     */
    @TableField(value = "md_id")
    private String mdId;

    /**
     * 状态 0:正常 1:禁用
     */
    @TableField(value = "state")
    private Integer state;


    /**
     * 性别
     * 0:男 1:女
     * */
    @TableField(value = "sex")
    private Integer sex;

    /**
     * 邮箱
     * */
    @TableField(value = "email")
    private String email;
    /**
     * 学号
     * */
    @TableField(value = "student_id")
    private String studentId;

    @TableField(exist = false)
    private List<String> perms;
    @TableField(exist = false)
    private TRole roles;
    @TableField(exist = false)
    private List<TMenu> menus;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        TUser tUser = (TUser) o;
        return Objects.equals(userId, tUser.userId) && Objects.equals(username, tUser.username) && Objects.equals(password, tUser.password) && Objects.equals(nickname, tUser.nickname) && Objects.equals(avatar, tUser.avatar) && Objects.equals(classId, tUser.classId) && Objects.equals(createTime, tUser.createTime) && Objects.equals(updateTime, tUser.updateTime) && Objects.equals(phone, tUser.phone) && Objects.equals(mdId, tUser.mdId) && Objects.equals(state, tUser.state) && Objects.equals(sex, tUser.sex) && Objects.equals(email, tUser.email) && Objects.equals(studentId, tUser.studentId) && Objects.equals(perms, tUser.perms) && Objects.equals(roles, tUser.roles) && Objects.equals(menus, tUser.menus);
    }

    @Override
    public int hashCode() {
        return Objects.hash(userId, username, password, nickname, avatar, classId, createTime, updateTime, phone, mdId, state, sex, email, studentId, perms, roles, menus);
    }


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (perms != null && perms.size()>0){
            List<SimpleGrantedAuthority> authorities = perms.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
            return authorities;
        }
        return null;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}